package org.cyhz.standardize.service;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import javax.annotation.PostConstruct;

import org.cyhz.domain.AutoInfoVO;
import org.cyhz.util.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class AutoInfoUtil {

	public static String delimiter;
	public static Set<String> IGNORE_WORDS = new HashSet<String>();
	public static String IGNORE_WORD_String;
	public static String IGNORE_WORDS_DELIMITER;
	public static String REMOVE_BLANK;

	public static String IGNORE_WORDS_MODEL;

	@Autowired(required = true)
	public void setREMOVE_BLANK(
			@Value("#{configSettings['remove_blank']}") String remove_blank) {
		REMOVE_BLANK = remove_blank.toUpperCase();
	}

	@Autowired(required = true)
	public void setIGNORE_WORDS_MODEL(
			@Value("#{configSettings['ignore_words_model']}") String iGNORE_WORDS_MODEL) {
		IGNORE_WORDS_MODEL = iGNORE_WORDS_MODEL.toUpperCase();
	}

	@Autowired(required = true)
	public void setIGNORE_WORDS_DELIMITER(
			@Value("#{configSettings['ignore_words_delimiter']}") String iGNORE_WORDS_DELIMITER) {
		IGNORE_WORDS_DELIMITER = iGNORE_WORDS_DELIMITER;
	}

	@Autowired(required = true)
	public void setIGNORE_WORD_String(
			@Value("#{configSettings['ignore_words']}") String ignoreWords) {
		// String[] ws = ignoreWords.split(IGNORE_WORDS_DELIMITER);
		// for (int i = 0; i < ws.length; i++) {
		// String iw = ws[i];
		// IGNORE_WORDS.add(iw.trim());
		// }
		IGNORE_WORD_String = ignoreWords;
	}

	@PostConstruct
	private void init() {
		String[] ws = IGNORE_WORD_String.split(IGNORE_WORDS_DELIMITER);
		for (int i = 0; i < ws.length; i++) {
			String iw = ws[i];
			IGNORE_WORDS.add(iw.trim());
		}
	}

	@Autowired(required = true)
	public void setDelimiter(
			@Value("#{configSettings['t_c.delimiter']}") String delimiter) {
		AutoInfoUtil.delimiter = delimiter;
	}

	public static String getT(AutoInfoVO autoInfoVO) {

		StringBuilder sb = new StringBuilder();

		if (null == autoInfoVO.getTitle()) {
		} else {
			sb.append(autoInfoVO.getTitle().trim());
		}

		StringBuilder o = new StringBuilder(sb.toString());

		int len = sb.length();
		if ("ON".equals(IGNORE_WORDS_MODEL)) {
			for (Iterator<String> i = IGNORE_WORDS.iterator(); i.hasNext();) {
				String iw = (String) i.next();
				sb = new StringBuilder(sb.toString().replaceFirst(iw, ""));
				if (sb.length() < len) {
					Log.info("title replace " + iw + "->" + sb.toString());
					len = sb.length();
				}
			}

			if (o.toString().equals(sb.toString())) {
				// replace nothing
			} else {
				Log.info("title " + o.toString() + "->" + sb.toString());
			}
		}

		if ("ON".equals(REMOVE_BLANK)) {
			// TODO: 可用正则来删除
			return sb.toString().toUpperCase().trim().replaceAll(" ", "")
					.replaceAll("\t", "").replaceAll("\n", "");
		} else {
			return sb.toString().toUpperCase().trim();
		}
	}

	public static String getTC(AutoInfoVO autoInfoVO) {

		StringBuilder sb = new StringBuilder();

		if (null == autoInfoVO.getTitle()) {
		} else {
			sb.append(autoInfoVO.getTitle().trim());
		}

		sb.append(delimiter);

		if (null == autoInfoVO.getAutoName()) {
		} else {
			sb.append(autoInfoVO.getAutoName().trim());
		}

		StringBuilder o = new StringBuilder(sb.toString());

		int len = sb.length();
		if ("ON".equals(IGNORE_WORDS_MODEL)) {
			for (Iterator<String> i = IGNORE_WORDS.iterator(); i.hasNext();) {
				String iw = (String) i.next();

				sb = new StringBuilder(sb.toString().replaceFirst(iw, ""));

				if (sb.length() < len) {
					Log.info("tc replace " + iw + "-->" + sb.toString());
					len = sb.length();
				}

			}

			if (o.toString().equals(sb.toString())) {
				// replace nothing
			} else {
				Log.info("tc " + o.toString() + "-->" + sb.toString());
			}
		}

		if ("ON".equals(REMOVE_BLANK)) {
			// TODO: 可用正则来删除
			return sb.toString().toUpperCase().trim().replaceAll(" ", "")
					.replaceAll("\t", "").replaceAll("\n", "");
		} else {
			return sb.toString().toUpperCase().trim();

		}
	}
}
